﻿// DigitalDesign
// Project: InformationBoard
// Developer: Bobylev Dmitry
// Created: 13.11.2011
// Last change: 23.11.2011
// Description: methods of navigation page.

using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using GUIAdmin.AdminServiceReference;
using System.Collections.ObjectModel;

namespace GUIAdmin
{
    public partial class MainPage : UserControl
    {
        private AdminServiceClient adminClient;
        public int selectedIndex { get; set; }
        private ObservableCollection<StationNotEF> stationList;
        
        public MainPage()
        {
            InitializeComponent();
            adminClient = new AdminServiceClient();
            GUIAdmin.Views.Stations.stationList = new ObservableCollection<StationNotEF>();
            stationList = GUIAdmin.Views.Stations.stationList;
            StationsSelect.ItemsSource = stationList;
            adminClient.GetListOfStationsCompleted += FillStationsSelect;
            adminClient.GetListOfStationsAsync();
            GUIAdmin.Views.Stations.mainPage = this;            
        }

        public void FillStationsSelect(object sender, GetListOfStationsCompletedEventArgs e)
        {
            stationList.Clear();
            foreach (var station in e.Result)
            {
                stationList.Add(station);                
            }

            if (stationList.Count != 0 && selectedIndex != -1)
            {
                if (StationsSelect.Items.Count < selectedIndex + 1)
                {
                    StationsSelect.SelectedIndex = StationsSelect.Items.Count - 1;
                    selectedIndex = StationsSelect.Items.Count - 1;
                }
                else
                {
                    StationsSelect.SelectedIndex = selectedIndex;
                }
            }            
        }

        // После перехода в фрейме убедиться, что выбрана кнопка HyperlinkButton, представляющая текущую страницу
        private void ContentFrame_Navigated(object sender, NavigationEventArgs e)
        {            
            foreach (UIElement child in LinksStackPanel.Children)
            {
                HyperlinkButton hb = child as HyperlinkButton;
                if (hb != null && hb.NavigateUri != null)
                {
                    if (hb.NavigateUri.ToString().Equals(e.Uri.ToString()))
                    {
                        VisualStateManager.GoToState(hb, "ActiveLink", true);
                    }
                    else
                    {
                        VisualStateManager.GoToState(hb, "InactiveLink", true);
                    }
                }
            }
        }

        // Если во время навигации возникает ошибка, отобразить окно ошибки
        private void ContentFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
        {
            e.Handled = true;
            ChildWindow errorWin = new ErrorWindow(e.Uri);
            errorWin.Show();
        }

        private void StationsSelect_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (StationsSelect.SelectedItem != null)
            {
                this.DataContext = StationsSelect.SelectedItem;
                selectedIndex = StationsSelect.SelectedIndex;
                string a = ContentFrame.Content.GetType().ToString();

                //не обновлять если страница с вокзалами
                if(ContentFrame.Content.GetType()!= typeof(GUIAdmin.Views.Stations))
                    ContentFrame.Refresh();
            }
        }
    }
}